﻿using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class RetrieveChecklistProductInfoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveChecklistProductInfoRequest, RetrieveChecklistProductInfoResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        public int ProductId;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveChecklistProductInfoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveChecklistProductInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.ProductId == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                ProductId = request.ProductId;
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveChecklistProductInfoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                ChecklistProductInfoRecord record = new ChecklistProductInfoRecord();
                ProductHandbookProductDetail productDetail = new ProductHandbookProductDetail();
                productDetail = Providers.ProductHandbooks.RetrieveProductHandbookProductDetail(ProductId);
                if (productDetail != null)
                {
                    record = new ChecklistProductInfoRecord
                    {
                        ProductId = ProductId,
                        ProductName = productDetail.ProductName,
                        ProductNameEn = productDetail.ProductNameEn
                    };
                    List<ChecklistSubProduct> ListSubProduct = new List<ChecklistSubProduct>();
                    ListSubProduct = Providers.ProductHandbooks.RetrieveSubProductWithoutSubId(ProductId);
                    record.ListSubProduct = ListSubProduct.Select(x => new ChecklistSubProductRecord
                    {
                        SubProductId = x.SubProductId,
                        SubProductName = x.SubProductName,
                        SubProductNameEn = x.SubProductNameEn
                    }).ToList();
                }
                response.Record = record;
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
